// 导入swagger-jsdoc库及swagger-ui-express库
const swaggerJsdoc = require('swagger-jsdoc');
const swaggerUi = require('swagger-ui-express');
//导入express
const express = require('express');
//导入path
const path = require('path');
//配置swagger文档
const swaggerOptions = {
    swaggerDefinition: {
        info: {
            title: 'API文档',
            version: '1.0.0',
            description: 'API文档',
        },
    },
    apis: [path.join(__dirname, '../routes/*.js')], // 指定API文件
}

var swaggerJSON = function (req, res) {
    res.setHeader('Content-Type', 'application/json');
    res.send(swaggerSpec);
}
var swaggerSpec = swaggerJsdoc(swaggerOptions);
var swaggerInstall = function (app) {
    if (!app) {
        app = express();
    }
    //开放相关接口
    app.get('swagger.json', swaggerJSON);
    //使用swaggerSpec 生成swagger文档页面，并开放在指定路由
    app.use('/swagger', swaggerUi.serve, swaggerUi.setup(swaggerSpec));
}
module.exports = swaggerInstall;